package com.esoon.smartsee;


import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esoon.api.SmartSeeImpl;
import com.esoon.api.SmartSeeInterface;
import com.esoon.fragment.AFragment;
import com.esoon.fragment.BFragment;
import com.esoon.fragment.CFragment;
import com.esoon.fragment.DFragment;
import com.esoon.utils.AlertDialogCommon;
import com.mb.voip.api.MBVoipClientApp;
import com.mb.voip.api.VTCall;
import com.mb.voip.api.VTCallStateListener;
import com.mb.voip.api.VideoPhone;
import com.mb.voip.api.VideoPhoneDataBean;
import com.mb.voip.api.mbVTCallsManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AFragment.OnFragmentInteractionListener, BFragment.OnFragmentInteractionListener, CFragment.OnFragmentInteractionListener, DFragment.OnFragmentInteractionListener, VTCallStateListener {

    private final static String TAG = "MainActivity";
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private AFragment aFragment;
    private BFragment bFragment;
    private CFragment cFragment;
    private DFragment dFragment;
    @BindView(R.id.msgbutton)
    ImageButton msgbutton;
    @BindView(R.id.mybutton)
    ImageButton mybutton;
    @BindView(R.id.spacebutton)
    ImageButton spacebutton;
    @BindView(R.id.setbutton)
    ImageButton setbutton;
    private List<Fragment> fragmentList = new ArrayList<>();
    public mbVTCallsManager callsManager = mbVTCallsManager.getManager();
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tag);
        initView();
        initData();

    }


    private void initView() {
        ButterKnife.bind(this);
        msgbutton.setOnClickListener(this);
        mybutton.setOnClickListener(this);
        spacebutton.setOnClickListener(this);
        setbutton.setOnClickListener(this);
        msgbutton.setSelected(true);
        //设置按钮状态，改变颜色

    }

    private void initData() {
        //使用viewpager+fragment添加首页滑动选项
        setFragment();
        //初始化本地存储字段“smartsee”
        sharedPreferences = getSharedPreferences("smartsee", MODE_PRIVATE);

        //初始化视频
        SmartSeeInterface smartSeeInterface = new SmartSeeImpl();
        smartSeeInterface.ESClientInitialize(this, sharedPreferences.getString("server", "47.103.45.89"), sharedPreferences.getString("userName", "10007"), sharedPreferences.getString("port", "5070"), this);


    }


    private void setFragment() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                changetab(position);
                /*滑动监听位置*/
            }

            @Override
            public void onPageSelected(int position) {


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        /*滑动监听事件*/


        viewPager.setOffscreenPageLimit(4);
        aFragment = new AFragment();
        bFragment = new BFragment();
        cFragment = new CFragment();
        dFragment = new DFragment();

        fragmentList.add(aFragment);
        fragmentList.add(bFragment);
        fragmentList.add(cFragment);
        fragmentList.add(dFragment);
        /*关联fragment，加入到数组*/
        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };


        viewPager.setAdapter(pagerAdapter);

    }

    @Override
    public void onClick(View v) {

        changetab(v.getId());

    }

    private void changetab(int position) {
        switch (position) {
            case R.id.msgbutton:
                viewPager.setCurrentItem(0);
            case 0:
                msgbutton.setSelected(true);
                mybutton.setSelected(false);
                spacebutton.setSelected(false);
                setbutton.setSelected(false);
                break;

            case R.id.mybutton:
                viewPager.setCurrentItem(1);
            case 1:
                msgbutton.setSelected(false);
                mybutton.setSelected(true);
                spacebutton.setSelected(false);
                setbutton.setSelected(false);

                break;
            case R.id.spacebutton:
                viewPager.setCurrentItem(2);
            case 2:

                msgbutton.setSelected(false);
                mybutton.setSelected(false);
                spacebutton.setSelected(true);
                setbutton.setSelected(false);

                break;
            case R.id.setbutton:
                viewPager.setCurrentItem(3);
            case 3:

                msgbutton.setSelected(false);
                mybutton.setSelected(false);
                spacebutton.setSelected(false);
                setbutton.setSelected(true);

                break;

        }

    }
    /*核心方法，根据监听改变选中的fragment和button*/

    @Override
    public void onFragmentInteraction(Uri uri) {

    }


    @Override
    public void onVTCallStateChanged(VideoPhone videoPhone, int i, int state, int i2, int i3, Object o) {

        Log.e(TAG, "call state is : " + state);
        switch (state) {

            case VideoPhoneDataBean.MBVoipCall_Registration:

                Toast.makeText(this, sharedPreferences.getString("userName","")+"注册成功", Toast.LENGTH_SHORT).show();

                break;


            case VideoPhoneDataBean.MBVoipCall_Initialization:

                Toast.makeText(this, "初始化成功", Toast.LENGTH_SHORT).show();
                break;


            case VideoPhoneDataBean.MBVoipCall_Unregistration:
                Toast.makeText(this, "结束注册", Toast.LENGTH_SHORT).show();
                break;

            case VideoPhoneDataBean.MBVoipCall_RegistrationFailed:
                Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
                break;

            case VideoPhoneDataBean.MBVoipCall_Inviting:

                break;

            case VideoPhoneDataBean.MBVoipCall_Offering:

                Toast.makeText(this, "正在响铃", Toast.LENGTH_SHORT).show();

                VTCall call = callsManager.getActiveCall();
                MBVoipClientApp client = MBVoipClientApp.getVoipClientApp();
                int audio_direction = 3;
                int video_direction = 3;
                client.Answer(call.callid, audio_direction, video_direction);
                break;

            case VideoPhoneDataBean.MBVoipCall_Connected:
                Toast.makeText(this, "通话已连接", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, VideoActivity.class);
                startActivity(intent);
                break;
            case VideoPhoneDataBean.MBVoipCall_Disonnected:


                Toast.makeText(MainActivity.this, "通话已经挂断", Toast.LENGTH_LONG).show();
                if (AFragment.callingServiceDialog != null) {
                    AFragment.callingServiceDialog.dismiss();
                }
                if (BFragment.callingServiceDialog != null) {
                    BFragment.callingServiceDialog.dismiss();

                }
                if (CFragment.callingServiceDialog != null) {
                    CFragment.callingServiceDialog.dismiss();

                }

                break;
            case VideoPhoneDataBean.MBVoipJNIMakeCallFailed:

                Log.e(TAG, "呼叫失败");
                Toast.makeText(this, "呼叫失败，请稍后拨打", Toast.LENGTH_SHORT).show();
                break;


        }


    }
}
